import { EntityComponentTypes, ItemStack, Player } from "@minecraft/server";
import { GameComponent } from "@sapi-game/main";
import { PKParkourState } from "../pkParkourState";

export class PKItemStoreComponent extends GameComponent<PKParkourState> {
    initItems: ItemStack[] = [
        new ItemStack("minecraft:bow"),
        new ItemStack("minecraft:stick"),
        new ItemStack("minecraft:arrow", 64),
    ];
    items: (ItemStack | undefined)[] = new Array(36);

    override onAttach(): void {
        this.initItems[1].nameTag = "切换玩家";
        //初始化
        for (let i = 0; i < this.initItems.length; i++) {
            this.items[i] = this.initItems[i];
        }
    }

    loadItem(player: Player) {
        const container = player.getComponent(EntityComponentTypes.Inventory)?.container;
        if (!container) return;
        container.clearAll();
        for (let i = 0; i < this.items.length; i++) {
            const item = this.items[i];
            if (item) {
                container.setItem(i, item);
            }
        }
    }

    storeItem(player: Player) {
        const container = player.getComponent(EntityComponentTypes.Inventory)?.container;
        if (!container) return;
        for (let i = 0; i < container.size; i++) {
            this.items[i] = container.getItem(i);
        }
    }
}
